package nc.tb.cube;

import java.util.Comparator;

import nc.tb.cube.cell.DataCell;
import nc.tb.dim.DimDef;
import nc.tb.dim.DimMember;

public class DataCellRowComparator implements Comparator<DataCell> {
	
	private DimDef[] rowDimDef;

	public DataCellRowComparator(DimDef[] ddf) {
		super();
		rowDimDef = ddf;
	}

	public int compare(DataCell d1, DataCell d2) {
		int i1 = 0, i2 = 0;
		if (d1 == null)		i1 = -1;
		if (d2 == null)		i2 = -1;
		if (i1 < 0 || i2 < 0)
			return i1 - i2;
		DimVector dv1 = d1.getDimVector();
		DimVector dv2 = d2.getDimVector();
		for (int i=0; i<rowDimDef.length; i++) {
			DimMember dm1 = dv1.getDimMemberByDimDef(rowDimDef[i]);
			DimMember dm2 = dv2.getDimMemberByDimDef(rowDimDef[i]);
			if (dm1 == null && dm2 == null)
				continue;
			if (dm1 != null && dm2 != null) {
				int idx = dm1.getIdx() - dm2.getIdx();
				if (idx == 0)
					continue;
				else
					return idx;
			}
			if (dm1 != null)	return 1;
			if (dm2 != null)	return -1;
		}
		return 0;
	}

}
